home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / ShapePart Browser ƒ / ShapeSetup.c < prev    next >
Encoding:
Text File  |  1996-04-15  |  9.0 KB  |  356 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    ShapeSetup.c
  3.  *
  4.  *    Robert Dierkes,  April 26, 1993
  5.  *
  6.  *    Change History:
  7.  *
  8.  *       4/93    ???        New
  9.  *       4/96    bob        Updated #includes to support changed GX Library names.
  10.  *                    Changed fixed to Fixed.
  11.  *                    Changed boolean to Boolean.
  12.  *                    Added the copyright info.
  13.  *
  14.  *
  15.  *        © Apple Computer, Inc. 1990 - 1996  All rights reserved
  16.  *
  17.  */
  18.  
  19.  
  20. #undef    MAC_HEADERS
  21.  
  22.  
  23. /*------------------*/
  24. /*    Include Files    */
  25. /*------------------*/
  26. #ifndef    MAC_HEADERS
  27.     #include <Memory.h>
  28.     #include <Windows.h>
  29.     #include <Controls.h>
  30. #endif    MAC_HEADERS
  31.  
  32. #include <GXGraphics.h>
  33. #include <GXMath.h>
  34. #include "GraphicsLibraries.h"
  35. #include "FontLibrary.h"
  36.  
  37. #include "ShapeSetup.h"
  38.  
  39.  
  40. /*----------------------*/
  41. /*    Global Declarations    */
  42. /*----------------------*/
  43.  
  44.  
  45. /*------------------------------*/
  46. /*    External Declarations     */
  47. /*------------------------------*/
  48.  
  49.  
  50. /*------------------------------*/
  51. /*       Local ProtoTypes       */
  52. /*------------------------------*/
  53.  
  54.  
  55.     Boolean
  56. CreateShapesFrame (WindowPtr pWindow, register long numRows, register long numCols, gxShape *pBoxSet)
  57. {
  58.     #define        kSixteenthInch    (IntToFixed (4) + kFixOneHalf)
  59.  
  60.     gxRectangle        bounds;
  61.     register
  62.     long            r, c;
  63.     gxShape            box;
  64.     Fixed            side,
  65.                     left,
  66.                     top;
  67.  
  68.     if (numRows == 0  ||  numCols == 0)
  69.     {
  70.         DebugStr ("\pCreateShapesFrame: Number of rows or columns is zero");
  71.         return (false);
  72.     }
  73.  
  74.     /* Find side of each enclosing box */
  75.     side = FixedDivide (IntToFixed (pWindow->portRect.bottom - pWindow->portRect.top),
  76.                     IntToFixed (numCols)) - kSixteenthInch;
  77.  
  78.     *pBoxSet = GXNewShape (gxEmptyType);
  79.     for (r = 0, top = kSixteenthInch + (kSixteenthInch>>1); r < numRows; r++, top += side)
  80.     {
  81.         for (c = 0, left = kSixteenthInch + (kSixteenthInch>>1); c < numCols; c++, left += side)
  82.         {
  83.             box = NewShape4 (gxRectangleType, left, top, left + side, top + side);
  84.             GXInsetShape (box, kSixteenthInch);
  85.             GXSetShapeFill (box, gxEvenOddFill);
  86.             AddToShape (*pBoxSet, box);
  87.             GXDisposeShape (box);
  88.         }
  89.     }
  90.     SetShapeCommonColor (*pBoxSet, grayish + silver);
  91.  
  92.     return (true);
  93. }
  94.  
  95.  
  96.     Boolean
  97. CreateHitTestShapes (WindowPtr pWindow, gxShape **h1stHitShape, gxShape *pBoxSet, register long shapeCount)
  98. {
  99.     gxShape            *pShape;
  100.     register
  101.     long            shapeNum;
  102.     gxRectangle        bounds;
  103.  
  104.     if (shapeCount <= 0)
  105.     {
  106.         DebugStr ("\pCreateHitTestShapes: Number of shapes is <= zero");
  107.         return (false);
  108.     }
  109.  
  110.     if (h1stHitShape == nil)
  111.     {
  112.         DebugStr ("\pCreateHitTestShapes: Shape list handle is nil");
  113.         return (false);
  114.     }
  115.  
  116.     /* Allocate space for hit shapes */
  117.     *h1stHitShape = pShape = (gxShape *) NewPtr (shapeCount * sizeof (gxShape));
  118.     if (*h1stHitShape == nil)
  119.     {
  120.         DebugStr ("\pCreateHitTestShapes: NewPtr for shapes failed");
  121.         return (false);
  122.     }
  123.  
  124.     for (shapeNum = 0; shapeNum < shapeCount; shapeNum++, pShape++)
  125.     {
  126.         switch (shapeNum)
  127.         {
  128.         case kTrapizoidPolygon:
  129.             *pShape = NewShape4 (gxRectangleType, 0, 0, kHalfInch, kHalfInch);
  130.             SkewShapeAboutCenter (*pShape, -kFixOneHalf, 0);
  131.             SetShapeCommonColor (*pShape, light + blue);
  132.             break;
  133.  
  134.         case kOvalPath:
  135.             bounds.left        =
  136.             bounds.top        = 0;
  137.             bounds.right    =
  138.             bounds.bottom    = kHalfInch;
  139.             *pShape = NewOval (&bounds);
  140.  
  141.             GXSetShapeFill (*pShape, gxClosedFrameFill);
  142.             GXSetShapePen (*pShape, IntToFixed (15));
  143.             SetShapeCommonColor (*pShape, rose_madder);
  144.             break;
  145.  
  146.         case kArrowPolygon:
  147.             {
  148.                 Fixed    arrow[] = {1L, 7L,    0,                IntToFixed (5),
  149.                                             0,              IntToFixed (15),
  150.                                               IntToFixed (12),        IntToFixed (14),
  151.                                                 IntToFixed (10),    IntToFixed (20),
  152.                                                   IntToFixed (27),    IntToFixed (10),
  153.                                                 IntToFixed (10),    0,
  154.                                               IntToFixed (12),        IntToFixed (6)};
  155.                 gxJoinRecord    joinRec;
  156.  
  157.                 *pShape    = GXNewPolygons ((gxPolygons *) arrow);
  158.  
  159.                 joinRec.attributes = gxCurveJoin;
  160.                 joinRec.join = nil;
  161.                 joinRec.miter = 0;
  162.                 GXSetShapeJoin (*pShape, &joinRec);
  163.                 GXSetShapeFill (*pShape, gxClosedFrameFill);
  164.                 GXSetShapePen (*pShape, IntToFixed (12));
  165.  
  166.                 RotateShapeAboutCenter (*pShape, IntToFixed (-45));
  167.                 SetShapeCommonColor (*pShape, turquoise);
  168.             }
  169.             break;
  170.  
  171.         case kPatternedPolygon:
  172.             {
  173.                 Fixed    star[] = {1L, 5L,    IntToFixed (3),    IntToFixed (18),
  174.                                             IntToFixed (9),    0,
  175.                                             IntToFixed (15),IntToFixed (18),
  176.                                             0,                IntToFixed (6),
  177.                                             IntToFixed (18),IntToFixed (6)};
  178.                 gxPatternRecord    patternRec;
  179.  
  180.                 *pShape = NewShape4 (gxRectangleType, 0, 0, kHalfInch, kHalfInch);
  181.                 RotateShapeAboutCenter (*pShape, IntToFixed (-6));
  182.                 SetShapeCommonColor (*pShape, gxWhite);
  183.  
  184.                 patternRec.pattern = GXNewPolygons ((gxPolygons *) star);
  185.                 GXSetShapeFill (patternRec.pattern, gxWindingFill);
  186.                 patternRec.u.x =  kQuarterInch;
  187.                 patternRec.u.y = -kQuarterInch;
  188.                 patternRec.v.x =  kQuarterInch;
  189.                 patternRec.v.y =  kEigthInch;
  190.                 GXSetShapePattern (*pShape, &patternRec);
  191.                 GXDisposeShape (patternRec.pattern);
  192.             }
  193.             break;
  194.  
  195.         case kWordGlyphs:
  196.             {
  197.                 #define    kText    "GX"
  198.                 gxPoint    position = {0, 0};
  199.  
  200.                 *pShape = NewCString (kText, &position);
  201.                 GXSetShapeType (*pShape, gxGlyphType);
  202.                 GXSetShapeTextSize (*pShape, IntToFixed (66));
  203.                 SetStyleCNamedFont (GXGetShapeStyle (*pShape), "Hoefler Italic");
  204.                 SetShapeCommonColor (*pShape, indigo);
  205.             }
  206.             break;
  207.  
  208.         case kArchCurve:
  209.             {
  210.                 Fixed    arch[] = {    0,                kHalfInch,
  211.                                     kEigthInch,        0,
  212.                                     kQuarterInch,    kHalfInch};
  213.  
  214.                 *pShape = GXNewCurve ((gxCurve *) arch);
  215.                 GXSetShapePen (*pShape, IntToFixed (20));
  216.                 SetShapeCommonColor (*pShape, light + maroon);    /* light + violet */
  217.                 ScaleShapeAboutCenter (*pShape, 5 * fixed1, 3 * fixed1);
  218.             }
  219.             break;
  220.  
  221.         case kCapsLine:
  222.             {
  223.                 #define    kFixOneQuarter    (fixed1 >> 2)
  224.                 #define    kFix3Quarters    ((3 * fixed1) >> 2)
  225.  
  226.                 Fixed    star[] = {1L, 5L,    -fixed1-kFixOneQuarter,    -3*fixed1+kFixOneQuarter,
  227.                                             -fixed1-kFixOneQuarter,     2*fixed1+kFixOneHalf,
  228.                                              fixed1+kFix3Quarters,    -2*fixed1,
  229.                                             -3*fixed1,                 0,
  230.                                              fixed1+kFix3Quarters,     fixed1+kFix3Quarters};
  231.                 gxCapRecord    cap;
  232.                 gxRectangle    endRect;
  233.  
  234.                 *pShape = NewShape4 (gxLineType, fixed1, 0, 0, fixed1);
  235.                 GXSetShapePen (*pShape, IntToFixed (8));
  236.                 SetShapeCommonColor (*pShape, viridian_light);
  237.  
  238.                 endRect.left   = 0;
  239.                 endRect.top    = kFix3Quarters;
  240.                 endRect.right  = 2 * kFix3Quarters;
  241.                 endRect.bottom = -kFix3Quarters;
  242.                 cap.endCap   = GXNewRectangle (&endRect);
  243.                 cap.startCap = GXNewPolygons ((gxPolygons *) star);
  244.  
  245.                 cap.attributes = 0;
  246.                 GXSetShapeCap (*pShape, &cap);
  247.                 GXDisposeShape (cap.startCap);
  248.                 GXDisposeShape (cap.endCap);
  249.             }
  250.             break;
  251.  
  252.         case kDashedOvalPath:
  253.             {
  254.                 gxDashRecord    dashRec;
  255.  
  256.                 bounds.left        =
  257.                 bounds.top        = 0;
  258.                 bounds.right    =
  259.                 bounds.bottom    = kQuarterInch;
  260.  
  261.                 dashRec.attributes = gxBreakDash + gxAutoAdvanceDash;
  262.                 dashRec.dash    = NewOval (&bounds);
  263.                 dashRec.advance    = kHalfInch;
  264.                 dashRec.phase    = 0;
  265.                 dashRec.scale    = kQuarterInch;
  266.  
  267.                 bounds.left        =
  268.                 bounds.top        = 0;
  269.                 bounds.right    =
  270.                 bounds.bottom    = kHalfInch + kEigthInch;
  271.                 *pShape = NewOval (&bounds);
  272.  
  273.                 GXSetShapeDash (*pShape, &dashRec);
  274.                 GXSetShapeFill (*pShape, gxClosedFrameFill);
  275.                 GXSetShapeStyleAttributes (*pShape, gxOutsideFrameStyle);
  276.                 GXSetShapePen (*pShape, kQuarterInch);
  277.                 SetShapeCommonColor (*pShape, light + apple_yellow);
  278.                 GXDisposeShape (dashRec.dash);
  279.             }
  280.             break;
  281.  
  282.         case kJoinPath:
  283.             {
  284.                 Fixed    spokes[] = {1L, 8L, 0x55000000,                0,   IntToFixed (4),
  285.                                                         IntToFixed (4),  IntToFixed (4),
  286.                                                         IntToFixed (4),                0,
  287.                                                         IntToFixed (4),  IntToFixed (4),
  288.                                                         IntToFixed (8),  IntToFixed (4),
  289.                                                         IntToFixed (4),  IntToFixed (4),
  290.                                                         IntToFixed (4),  IntToFixed (8),
  291.                                                         IntToFixed (4),  IntToFixed (4)};
  292.                 *pShape    = GXNewPaths ((gxPaths *) spokes);
  293.                 SetShapeCommonColor (*pShape, light + orange);
  294.             }
  295.             break;
  296.         }
  297.  
  298.         if (*pShape)
  299.         {
  300.             GXGetShapeBounds (*pBoxSet, shapeNum + 1, &bounds);
  301.  
  302.             if (shapeNum == kWordGlyphs)
  303.                 GXMoveShapeTo (*pShape, bounds.left + kEigthInch, bounds.bottom - kHalfInch - kEigthInch);
  304.             else if (shapeNum == kArchCurve)
  305.                 CenterShape (*pShape, &bounds);
  306.             else
  307.                 GXSetShapeBounds (*pShape, &bounds);
  308.  
  309.             if (shapeNum == kTrapizoidPolygon  ||
  310.                 shapeNum == kOvalPath  ||
  311.                 shapeNum == kPatternedPolygon  ||
  312.                 shapeNum == kDashedOvalPath)
  313.                 GXInsetShape (*pShape, kQuarterInch);
  314.  
  315.             if (shapeNum == kArrowPolygon)
  316.                 ScaleShapeAboutCenter (*pShape, kFixOneHalf + (kFixOneHalf >> 1), kFixOneHalf + (kFixOneHalf >> 1));
  317.  
  318.             if (shapeNum == kCapsLine)
  319.                 ScaleShapeAboutCenter (*pShape, kFixOneHalf, kFixOneHalf);
  320.  
  321.             if (shapeNum == kJoinPath)
  322.                 ScaleShapeAboutCenter (*pShape, 7 * (kFixOneHalf >> 2), 7 * (kFixOneHalf >> 2));
  323.  
  324.             GXCacheShape (*pShape);
  325.         }
  326.     }
  327.  
  328.     return (true);
  329. }
  330.  
  331.  
  332.     void
  333. DisposeHitTestShapes (gxShape **h1stHitShape, gxShape *pBoxes)
  334. {
  335.     register
  336.     gxShape    *pShape;
  337.     long    shapeCount;
  338.  
  339.     if (pBoxes  &&  *pBoxes)
  340.     {
  341.         if (h1stHitShape  &&  *h1stHitShape)
  342.         {
  343.             shapeCount = GXCountShapeContours (*pBoxes);
  344.             pShape = *h1stHitShape + shapeCount - 1;
  345.  
  346.             while (shapeCount--)
  347.                 DisposeShapeAt (pShape--);
  348.  
  349.             DisposePtr ((Ptr) *h1stHitShape);
  350.             *h1stHitShape = nil;
  351.         }
  352.  
  353.         DisposeShapeAt (pBoxes);
  354.     }
  355. }
  356.